'''
@author: saaj
@license: LGPL
'''


import os.path


def _merge(target, source):
  result = target.copy() # shallow copy
  stack  = [(result, source)]
  while stack:
    currentTarget, currentSource = stack.pop()
    for key in currentSource:
      if key not in currentTarget:
        currentTarget[key] = currentSource[key] # appending
      else:
        if isinstance(currentTarget[key], dict) and isinstance(currentSource[key], dict):
          currentTarget[key] = currentTarget[key].copy() # nested dict copy
          stack.append((currentTarget[key], currentSource[key]))
        else:
          currentTarget[key] = currentSource[key] # overriding
          
  return result

_path   = os.path.abspath(os.path.dirname(__file__))
_config = {
  'global' : {
    # http server
    'server.socket_host' : "127.0.0.1",
    'server.socket_port' : 8080,
    'server.thread_pool' : 8,
    # file change reload
    'engine.autoreload_on' : False,
    # url trailing slash
    'tools.trailing_slash.on' : False,
    # logging
    'log.access_file' : os.path.join(_path, "variable/log/access_back.log"), 
    'log.error_file'  : os.path.join(_path, "variable/log/error_back.log"),
  },
  '/' : {
    'tools.encode.encoding' : "utf-8"
  }
}


production = _merge(_config, {
  'global' : {
    'server.thread_pool' : 16
  },
  'database' : {
    'host'    : "127.0.0.1",
    'user'    : "webapp",
    'passwd'  : "weBaPPpwD",
    'db'      : "webapp",
    'charset' : "utf8"
  }
})

development = _merge(_config, {
  'database' : {
    'host'    : "127.0.0.1",
    'user'    : "guest",
    'passwd'  : "",
    'db'      : "webapp",
    'charset' : "utf8"
  },
  '/resource' : {
    'tools.staticdir.on'  : True,
    'tools.staticdir.dir' : os.path.join(_path, "public/resource")   
  } 
})

test_suite = _merge(_config, {
  'database' : {
    'host'    : "127.0.0.1",
    'user'    : "guest",
    'passwd'  : "",
    'db'      : "webapp_test",
    'charset' : "utf8"
  }
})